﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;

using System.Windows.Forms;

namespace UIT_CV_Management
{
    public class QuaTrinhHoatDongKHKT
    {
        public String ID;
        public String MaQuaTrinhHoatDongKHKT;
        public String TenDeAn;
        public DateTime ThoiGian;
        public String NoiCongTac;
        public String DanhGiaKetQua;

        public QuaTrinhHoatDongKHKT(String _MaQuaTrinhHoatDongKHKT, String _TenDeAn, DateTime _ThoiGian,
            String _NoiCongTac, String _DanhGiaKetQua)
        {
            ID = Database.getID("QuaTrinhHoatDongKHKT", "ID");
            MaQuaTrinhHoatDongKHKT = _MaQuaTrinhHoatDongKHKT;
            TenDeAn = _TenDeAn;
            ThoiGian = _ThoiGian;
            NoiCongTac = _NoiCongTac;
            DanhGiaKetQua = _DanhGiaKetQua;
        }

        public QuaTrinhHoatDongKHKT(String _ID)
        {
            QueryResult result = Database.shareDatabase.Query("SELECT * FROM QuaTrinhHoatDongKHKT " +
                "WHERE ID = '" + "'");
            if (result.ReturnCode == 200)
            {
                DataRow row = result.Table.Rows[0];
                ID = _ID;
                MaQuaTrinhHoatDongKHKT = row["MaQuaTrinhHDKHKT"].ToString();
                TenDeAn = row["TenDeAn"].ToString();
                ThoiGian = row.Field<DateTime>("ThoiGian");
                NoiCongTac = row["NoiCongTac"].ToString();
                DanhGiaKetQua = row["DanhGiaKetQua"].ToString();
                
            }
            else
                MessageBox.Show(result.Error);
        }
        
        public Boolean Save()
        {
            QueryResult result;
            if (Database.isDupplicate("QuaTrinhHoatDongKHKT", "ID", ID))
            {
                //Update
                result = Database.shareDatabase.Query("UPDATE QuaTrinhHoatDongKHKT SET " +
                    "MaQuaTrinhHoatDongKHKT = '" + MaQuaTrinhHoatDongKHKT + "', " +
                    "TenDeAn = '" + TenDeAn + "', " +
                    "ThoiGian = '" + Database.sqlDateTime(ThoiGian) + "', " +
                    "NoiCongTac = '" + NoiCongTac + "', " +
                    "DanhGiaKetQua = '" + DanhGiaKetQua + "'");
                if (result.ReturnCode == 200)
                    return true;
                else
                    return false;
            }
            else
            {
                //Insert
                result = Database.shareDatabase.Query("INSERT INTO QuaTrinhHoatDongKHKT VALUES(" +
                    "'" + ID + "', " +
                    "'" + MaQuaTrinhHoatDongKHKT + "', " +
                    "'" + TenDeAn + "', " +
                    "'" + Database.sqlDateTime(ThoiGian) + "', " +
                    "'" + NoiCongTac + "', " +
                    "'" + DanhGiaKetQua + "')");
                if (result.ReturnCode == 200)
                    return true;
                else
                    return false;
            }
        }

        public Boolean Delete()
        {
            QueryResult result = Database.shareDatabase.Query("DELETE FROM QuaTrinhHoatDongKHKT " +
                "WHERE ID = '" + ID + "'");
            if (result.ReturnCode == 200)
                return true;
            else
                return false;
        }

        public static List<QuaTrinhHoatDongKHKT> getAllQuaTrinhHoatDongKHKT(String _MaQuaTrinhHoatDongKHKT)
        {
            QueryResult result = Database.shareDatabase.Query("SELECT * FROM QuaTrinhHoatDongKHKT " +
                "WHERE MaQuaTrinhHoatDongKHKT = '" + _MaQuaTrinhHoatDongKHKT + "'");
            if (result.ReturnCode == 200)
            {
                List<QuaTrinhHoatDongKHKT> _result = new List<QuaTrinhHoatDongKHKT>();
                foreach(DataRow dtr in result.Table.Rows)
                    _result.Add(new QuaTrinhHoatDongKHKT(dtr["ID"].ToString()));
                return _result;
            }
            else
                return null;
        }
    }
}
